package cz.muni.fi.pa165.service.local.impl;

import cz.muni.fi.pa165.dao.EmployeeDAO;
import cz.muni.fi.pa165.dto.EmployeeDTO;
import cz.muni.fi.pa165.entity.Employee;
import cz.muni.fi.pa165.service.local.EmployeeServiceLocal;
import cz.muni.fi.pa165.shiro.ShiroSecured;
import cz.muni.fi.pa165.transfers.EmployeeT;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import org.apache.shiro.authz.annotation.RequiresRoles;
/**
 *
 * @author Jakub Janco
 */
@ApplicationScoped
@Named
@ShiroSecured
@RequiresRoles("Manager")
public class EmployeeService implements EmployeeServiceLocal{

    @EJB
    private EmployeeDAO employeeDAO;
    
    @Override
    public Long createEmployee(EmployeeDTO employee) {
        Employee entity = EmployeeT.dTOtoEntity(employee);
        employeeDAO.createEmployee(entity);
        return entity.getId();
    }

    @Override
    public void removeEmployee(EmployeeDTO employee) {
        employeeDAO.removeEmployee(EmployeeT.dTOtoEntity(employee));
    }

    @Override
    public void updateEmployee(EmployeeDTO employee) {
        employeeDAO.updateEmployee(EmployeeT.dTOtoEntity(employee));
    }

    @Override
    @Produces
    @Named("deliveryBoys")
    public List<EmployeeDTO> getAllEmployees() {
        List<Employee> employees = employeeDAO.getAllEmployees();
        List<EmployeeDTO> employeesDTO = new ArrayList<EmployeeDTO>();
        for(Employee employee : employees) {
            employeesDTO.add(EmployeeT.entityToDTO(employee));
        }
        return employeesDTO;
    }

    @Override
    public EmployeeDTO getEmployeeById(Long id) {
        return EmployeeT.entityToDTO(employeeDAO.getEmployeeById(id));
    }

}
